package com.zhou.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhou.dao.UserDao;
import com.zhou.domain.DTO.UserLoginDTO;
import com.zhou.domain.User;
import com.zhou.server.UserServer;
import com.zhou.util.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServerImpl implements UserServer {

    @Autowired
    private UserDao userDao;

    @Autowired
    private JedisUtil jedisUtil;

    //根据ID查询
    @Override
    public User getById(Integer id) {
        User user1 = jedisUtil.RedisGet(id + "", User.class);
        //int a = 1/0;
        if (user1==null){
            User user = userDao.getById(id);
            jedisUtil.RedisSet(id + "",user);
            user1=user;
        }

        return user1;
    }

    //添加用户
    @Override
    public String save( User user) {
        userDao.save(user);
        return "添加成功";
    }

    /**
     * 根据ID删除用户
     * @param id
     */
    @Override
    public void delete(int id) {
        userDao.deleteById(id);
    }

    //修改用户
    @Override
    public void updata(User user) {
        userDao.updateById(user);
    }

    //用户登录
    @Override
    public String login(UserLoginDTO userLoginDTO) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getName,userLoginDTO.getName()).eq(User::getPassword,userLoginDTO.getPasswoed());
        User user = userDao.selectOne(lqw);
        if(user==null){
            return "登录失败";
        }
        return "登录成功";
    }


}
